<?php 

class Result_List_Tag extends H2o_Node {
    private $nodelist;
    
    function __construct($argstring, $parser, $position = 0) {
        $this->args = H2o_Parser::parseArguments($argstring);        

        $this->filename = 'admin/change_list_results.html';
        $this->nodelist = $parser->runtime->loadSubTemplate($this->filename, $parser->options);
        $parser->storage['templates'] = array_merge(
            $this->nodelist->parser->storage['templates'], $parser->storage['templates']
        );
        $parser->storage['templates'][] = $this->filename;
    }

    function render($context, $stream) {
        $cl = $context->resolve($this->args[0]);
        
        $resultHeaders = array();
        
        foreach ($cl->list_display as $field_name) {
            
            $text = substr($field_name, strpos($field_name, '.')+1, strlen($field_name));
            $text = str_replace('_', ' ', $text);
            
            $langKey = encode_lang_key($text);
            
            if (isset($GLOBALS['LANG'][$langKey])){
                $text = $GLOBALS['LANG'][$langKey]['msgstr'];
            }
            
            $resultHeaders[] = array(
               'text' => $text,
               'sortable' => false,
               'url' => '',
               'class_attrib' => ' class="manage-column"',
            );
        }       
        
        $context['result_headers'] = $resultHeaders;
        
        //print_r($cl->result_list);

        
        
        $results = array();
        
        $tmpResultList = $cl->paginator->execute(array(), Doctrine_Core::HYDRATE_SCALAR);
        
        foreach ($tmpResultList as $resultItem) {
            
            $rowActionsArr = array();
        
            if (is_array($cl->row_actions)){
                foreach ($cl->row_actions as $rowAction) {
                	$rowActionName = $rowAction;
                	$rowActionUrl = './'.$resultItem['o_id'].'/'.$rowActionName.'/';
                	$confirmTxt = '';
                	
                	if (is_array($rowAction)){
                		$rowActionName = $rowAction['name'];
                		$rowActionUrl = $GLOBALS['SITE_URL'].$rowAction['url'].$resultItem['o_id'].'/';
                	}
                	
                    
                    
                    if ($rowActionName == 'delete'){
                        $confirmTxt = "if ( confirm('Bu kaydı silmek istediğinizden eminmisiniz.') ) return true; else return false; ";
                    }
                    
                    
                    
                    $rowActionsArr[] = '<span class="'.$rowActionName.'"><a href="'.$rowActionUrl.'" onclick="'.$confirmTxt.'" title="'.$rowActionName.' this record">'.ucfirst($rowActionName).'</a></span>';
                }
            }
            
            $resultItems = array('<th scope="row" class="check-column"><input type="checkbox" name="result_items[]" value="'.$resultItem['o_id'].'" /></th>');
            
            $isFirstRow = true;
            foreach ($cl->list_display as $fieldName) {             
                $tmpFieldName = str_replace('.', '_', $fieldName);
                $tmpResultItem = '';
                
                if (in_array($fieldName, $cl->list_display_links)){
                    $tmpResultItem = sprintf('<strong><a class="row-title" href="%s">%s</a></strong>', 
                                           './'.$resultItem['o_id'].'/',
                                           $resultItem[$tmpFieldName]);
                }else{
                    $tmpResultItem = $resultItem[$tmpFieldName];    
                }
                
                if ($isFirstRow){
                    $resultItems[] = sprintf('<td class="">%s<div class="row-actions">%s</div></td>', 
                                    $tmpResultItem, implode(' | ', $rowActionsArr));    
                }else {
                    $resultItems[] = sprintf('<td class="">%s<div class="row-actions">%s</div></td>', 
                                    $tmpResultItem, '');   
                }
                
                
                              
                
                $isFirstRow = false;
            }
            
            
            $results[] = $resultItems;
            
            
        }
        
        $context['results'] = $results;
        
        
        
        $this->nodelist->render($context, $stream);
    }
}

H2o::addTag(array('result_list'));
